package util;

import org.apache.log4j.Logger;

import taper.controller.ComPort;
import taper.controller.MewtocolCommand;
import taper.controller.plc.TaperAdjustProcessor;

public class PLCSend {
	
	private static final Logger log = Logger.getLogger(PLCSend.class);
	
	public static void updatePLCTaper(String arm, String stationName, double value) {
		String dt;

		if (arm.equals("L"))dt = "02";else dt = "06";
		
		int plcValue = UnitConversion.inchesHgToPLC(value);
		try {
			log.info("updating taper plcValue: "+plcValue+"(Hg)");
			
			String plcResult = ComPort.getInstance().sendMewtocol(MewtocolCommand.writeRegister(stationName, dt, plcValue));
			log.info(MewtocolCommand.writeRegister(stationName, dt, plcValue));
			
			log.info("plcResult: "+plcResult);
			log.info("done updating taper");
		} catch (Exception e) {
			log.error("TaperAdjustProcessor:updatePLCTaper",e);
		}
	}
	
	public static void updatePLCPressure(String arm, String stationName, double psi)
	{
		String dt;
		if (arm.equals("L"))dt = "10";else dt = "14";
		   
		int analogValue = UnitConversion.psiToPLC(psi);
		String plcHex = UnitConversion.readeableToPlcHex(analogValue);
		String respond;
	   try
	   {
	    respond = ComPort.getInstance().sendMewtocol("%" + stationName + "#WDD000" + dt + "000" + dt + plcHex + "**");
	    log.info("%" + stationName + "#WDD000" + dt + "000" + dt + plcHex + "**");
		log.info("respond: "+respond);
		log.info("done updating Pressure");
	   }
	   catch (Exception e)
	    {
		   log.error("TaperAdjustProcessor:updatePLCTaper",e);
	     }
	  }

}
